package com.sokal.easycare.modules.picture.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.mapper.JsonMapper;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.utils.FastDFS.FastDFSClient;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.picture.entity.PicArticle;
import com.sokal.easycare.modules.picture.entity.PicArticleFile;
import com.sokal.easycare.modules.picture.service.PicArticleService;

/**
 * 图文内容Controller
 * @author liliangdong
 * @version 2017-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/picture/picArticle")
public class PicArticleController extends BaseController {

	@Autowired
	private PicArticleService picArticleService;
	
	@ModelAttribute
	public PicArticle get(@RequestParam(required=false) String id) {
		PicArticle entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = picArticleService.get(id);
		}
		if (entity == null){
			entity = new PicArticle();
		}
		return entity;
	}
	
	@RequiresPermissions("picture:picArticle:view")
	@RequestMapping(value = {"list", ""})
	public String list(PicArticle picArticle, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PicArticle> page = picArticleService.findPage(new Page<PicArticle>(request, response), picArticle); 
		model.addAttribute("page", page);
		return "modules/picture/picArticleList";
	}

	@RequiresPermissions("picture:picArticle:view")
	@RequestMapping(value = "form")
	public String form(PicArticle picArticle, Model model) {
		model.addAttribute("picArticle", picArticle);
		return "modules/picture/picArticleForm";
	}

	@RequiresPermissions("picture:picArticle:edit")
	@RequestMapping(value = "save")
	public String save(PicArticle picArticle, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, picArticle)){
			return form(picArticle, model);
		}
		picArticleService.save(picArticle);
		addMessage(redirectAttributes, "保存图文内容成功");
		return "redirect:"+Global.getAdminPath()+"/picture/picArticle/?repage";
	}
	
	@RequiresPermissions("picture:picArticle:edit")
	@RequestMapping(value = "delete")
	public String delete(PicArticle picArticle, RedirectAttributes redirectAttributes) {
		picArticleService.delete(picArticle);
		addMessage(redirectAttributes, "删除图文内容成功");
		return "redirect:"+Global.getAdminPath()+"/picture/picArticle/?repage";
	}
	
	/**
	 * 上传文章图片
	 * @param articleFile
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "uploadArticleFile")
	public String uploadArticleFile(@RequestParam(value = "articleFile") MultipartFile articleFile) {
		Map<String, Object> map = Maps.newHashMap();
		try {
			String fileUrl=FastDFSClient.uploadFile(articleFile, articleFile.getOriginalFilename());
			PicArticleFile picArticleFile=new PicArticleFile();
			picArticleFile.setFileUrl(fileUrl);
			picArticleFile.setFileName(articleFile.getOriginalFilename());
			//picArticleFile.setFastdfs_file_url(Global.getConfig("fastdfs_file_url"));
			map.put("success", true);
			map.put("picArticleFile", picArticleFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("success", false);
		}
		return JsonMapper.nonDefaultMapper().toJson(map);
	}

}